package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.settlement.constant.Subjects;
import com.yuyou.fn.settlement.entity.ChangeRecord;
import com.yuyou.fn.settlement.service.ISettlementService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by wens on 2017/4/19.
 */
public class CourseConsumeTask implements Runnable {

    @Resource
    private ISettlementService settlementService ;

    @Resource
    private IStudentRegService studentRegService ;

    @Resource
    private ICourseClassService courseClassService ;


    @Override
    public void run() {


        Example<CourseClass> courseClassExample = CourseClass.createExample();
        courseClassExample.createCriteria().andGreaterThan("completedLectureAmount",0 );
        List<CourseClass> courseClassList = courseClassService.findListByExample(courseClassExample);

        for(CourseClass courseClass : courseClassList ){

            Example<StudentReg> studentRegExample = StudentReg.createExample();
            studentRegExample.createCriteria().andEqualTo("classId",courseClass.getClassId() );

            List<StudentReg> studentRegList = studentRegService.findListByExample(studentRegExample);


            for(StudentReg studentReg : studentRegList ){
                if(studentReg.getChargingStatus() == 0 ){
                    continue;
                }

                if(courseClass.getCompletedLectureAmount()< studentReg.getStartAmount()){
                    continue;
                }


                List<ChangeRecord> futureIncomeChangeRecordList = settlementService.findChangeRecordsByTrackingId(studentReg.getRegId(), Subjects.FUTURE_INCOME, 0);
                List<ChangeRecord> consumeFutureIncomeChangeRecordList = settlementService.findChangeRecordsByTrackingId(studentReg.getRegId(), Subjects.FUTURE_INCOME, 0);

                BigDecimal futureIncomeAmount = toSum(futureIncomeChangeRecordList) ;
                BigDecimal deductedFutureIncomeAmount = toSum(consumeFutureIncomeChangeRecordList) ;

                if(futureIncomeAmount.add(deductedFutureIncomeAmount).compareTo(BigDecimal.ZERO) > 0 ){

                    //消耗讲数
                    int consumeLectureAmount  = courseClass.getCompletedLectureAmount() - studentReg.getStartAmount() + 1 ;
                    //报名讲数
                    int regLectureAmount  = studentReg.getEndAmount() - studentReg.getStartAmount() + 1 ;

                    //收入金额
                    BigDecimal realIncome = Utils.calProportion(studentReg.getTotalAmount(), new BigDecimal(consumeLectureAmount), new BigDecimal(regLectureAmount));

                    if(realIncome.compareTo(deductedFutureIncomeAmount.multiply(new BigDecimal(-1))) > 0 ){

                        //差值
                        BigDecimal offset = realIncome.subtract(deductedFutureIncomeAmount);
                        settlementService.bookConsumeCourse(courseClass.getBusTeamId(),offset,courseClass.getClassName(),courseClass.getClassId(),studentReg.getRegId());



                    }



                }

            }


        }


    }

    private BigDecimal toSum(List<ChangeRecord> changeRecordList) {

        BigDecimal amount  = new BigDecimal(0);
        for(ChangeRecord changeRecord : changeRecordList ){
            amount = amount.add(changeRecord.getAmount());
        }
        return amount ;
    }

    public static void main(String[] args) {
        System.out.println(new BigDecimal("0.0").compareTo(BigDecimal.ZERO));
    }
}
